package cn.itsource.order.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.CodeGenerateUtils;
import cn.itsource.basic.util.DistanceUtil;
import cn.itsource.order.domain.AdoptOrder;
import cn.itsource.order.domain.OrderAddress;
import cn.itsource.order.domain.ProductOrder;
import cn.itsource.order.domain.ProductOrderDetail;
import cn.itsource.order.mapper.OrderAddressMapper;
import cn.itsource.order.mapper.ProductOrderDetailMapper;
import cn.itsource.order.mapper.ProductOrderMapper;
import cn.itsource.order.service.IProductOrderService;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pay.constants.PayConstants;
import cn.itsource.product.domain.Product;
import cn.itsource.product.mapper.ProductMapper;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.domain.UserAddress;
import cn.itsource.user.mapper.UserAddressMapper;
import cn.itsource.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class ProductOrderServiceImpl extends BaseServiceImpl<ProductOrder> implements IProductOrderService {


    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ProductOrderMapper productOrderMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private ProductOrderDetailMapper productOrderDetailMapper;

    @Override
    public String submit(Map<String, Object> params, LoginInfo loginInfo) {
        Long productId = Long.valueOf(params.get("product_id").toString()); //服务id
        Integer productNum = Integer.valueOf(params.get("product_num").toString()); //服务数量
        Long addressId = Long.valueOf(params.get("address_id").toString());//用户地址id
        Integer serviceMethod = Integer.valueOf(params.get("service_method").toString()); //配送方式
        Integer payMethod = Integer.valueOf(params.get("pay_method").toString()); //支付方式
        //1 创建订单
        //1.1 订单本身-绑定最近店铺
        Product product = productMapper.loadById(productId);
        User user = userMapper.loadByLoginInfoId(loginInfo.getId());
        UserAddress userAddress = userAddressMapper.loadById(addressId);
        ProductOrder productOrder = initProductOrder(productNum, product, user, userAddress);
        productOrderMapper.save(productOrder);
        //1.2 订单详情-防止服务后面发生变更，特别是价格变更
        ProductOrderDetail detail = new ProductOrderDetail();
        BeanUtils.copyProperties(product,detail);
        detail.setId(null);
        detail.setOrder_id(productOrder.getId());
        productOrderDetailMapper.save(detail);
        //1.3 地址地址
        OrderAddress orderAddress = userAddress2OrderAddress(userAddress, productOrder);
        orderAddressMapper.save(orderAddress);
        //2 @TODO 创建支付单，并发起支付请求
        //3 @TODO 订单倒计时取消处理
        return null;
    }

    public OrderAddress userAddress2OrderAddress(UserAddress userAddress, ProductOrder productOrder){
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        orderAddress.setId(null);
        orderAddress.setOrder_id(productOrder.getId());
        orderAddress.setOrderSn(productOrder.getOrderSn());
        return orderAddress;
    }

    private ProductOrder initProductOrder(Integer productNum, Product product, User user, UserAddress userAddress) {
        ProductOrder productOrder = new ProductOrder();
        productOrder.setDigest("[摘要]服务"+product.getName()+"的订单！");
        productOrder.setState(0); //待支付
        productOrder.setPrice(product.getSaleprice()
                .multiply(new BigDecimal(productNum))); //价格=服务单价*数量
        productOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        //lastPayTime最后支付时间  当前时间+15分钟
        Date lastPayTime = new Date(System.currentTimeMillis()+ PayConstants.ORDER_TIMEOUT);
        productOrder.setLastPayTime(lastPayTime);
        productOrder.setProduct_id(product.getId());
        productOrder.setUser_id(user.getId());

        //获取最近店铺并完成绑定的
        Shop nearestShop = DistanceUtil.getNearestShop(DistanceUtil.getPoint(userAddress.getFullAddress()), shopMapper.loadAll());
        System.out.println(nearestShop);
        if (nearestShop!=null)
            productOrder.setShop_id(nearestShop.getId());

        return productOrder;
    }
}
